/**
 * Created by lenovo on 2019/11/24.
 */



$(document).ready(function () {
    $('.summernote').summernote({
        lang: 'zh-CN'
    });
});

$(function(){
    $('#ctype').change(function(){
        var data= $(this).val();
        if (data == "1") {
            $('#courText').hide();
            $('#pathDiv').show();
        } else {
            $('#courText').show();
            $('#pathDiv').hide();
        }
    });

    //$('.check').val('two').trigger('change');
});


var prefix = ctx + "self/course";
$("#form-course-add").validate({
    focusCleanup: true
});

function saveData() {
    var formData = new FormData();
    var cname = $("#cname").val();
    var cstatus = $("input[id='cstatus']").is(':checked') == true ? 0 : 1;
    var ctype =$("#ctype").val();
    var cdesc = $("#cdesc").val();

    if (cname == null || cname == "" ){
        $.modal.alertWarning("请填写课程名称");
        return false;
    }

    if (cdesc == null || cdesc == "" ){
        $.modal.alertWarning("请填写课程描述");
        return false;
    }

    if ($('#cimg')[0].files[0] == null) {
        $.modal.alertWarning("请先选择课程图片");
        return false;
    }

    formData.append('cname', cname);
    formData.append('cstatus', cstatus);
    formData.append('ctype', ctype);
    formData.append('cdesc', cdesc);
    formData.append('files', $('#cimg')[0].files[0]);

    if (ctype == "1") {
        if ($('#cpath')[0].files[0] == null) {
            $.modal.alertWarning("请先选择课程视频");
            return false;
        }
        formData.append('files', $('#cpath')[0].files[0]);
    } else {
        $("#eg").removeClass("no-padding");
        var aHTML = $('.summernote').summernote('code');
        if (aHTML == null || aHTML == "" ){
            $.modal.alertWarning("请编辑课程内容");
            return false;
        }
        $('.summernote').summernote('destroy');
        formData.append('ctext', aHTML);
    }

    console.log("courseadd param is :",formData);

    $.ajax({
        url: prefix + "/add",
        type: 'post',
        cache: false,
        data: formData,
        processData: false,
        contentType: false,
        dataType: "json",
        success: function(result) {
            $.operate.successCallback(result);
        }
    });
}


function submitHandler() {
    if ($.validate.form()) {
        saveData();
    }
}